<?php

namespace App\Http\Controllers\System;


use App\Criteria\System\Permission\PermissionBaseQueryCriteria;
use App\Http\Controllers\CurlRecordMethod;
use App\Http\Requests\System\PermissionRequest;
use App\Repositories\System\PermissionRepository;
use App\Validators\System\PermissionValidator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class PermissionsController extends Controller
{

    use CurlRecordMethod;

    protected $index_page='system.permission.permissionMain';
    protected $add_page='system.permission.permissionAdd';
    protected $edit_page='system.permission.permissionEdit';

    protected $repository;
    protected $validator;
    protected $request;

    public function __construct(PermissionRepository $repository,
                                  PermissionValidator $validator,
                                  PermissionRequest $request)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
        $this->request=$request;
    }


    public function getBaseQueryCriteria(){
        return new PermissionBaseQueryCriteria;
    }

    public function index(){
        $permissions=$this->repository->all();
        $tree_list = tree_to_list(list_to_tree($permissions['data'], $pk = 'id', $pid = 'parent_id'));
        return view($this->index_page,compact('tree_list'));
    }

    protected function get_add_data(){
        $parent_id=Input::get('parent_id');
        $parent=$this->repository->skipPresenter()->find($parent_id);
        return view($this->add_page,compact('parent'));
    }


    protected function get_edit_data(){
        $permission=$this->repository->skipPresenter()->find(Input::get('id'));
        $parent=$this->repository->skipPresenter()->find($permission->parent_id);
        return view($this->edit_page,compact('parent','permission'));
    }

    public function delete_data(){
        try {
            $this->validator->with(Input::all())->passesOrFail('DELETE');
            $this->repository->deleteByPrimaryKeys(Input::get('id'));
            return $this->return_json_success();
        } catch (ValidatorException $e) {
            return $this->return_json_error($e);
        }
    }
}
